<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Admin\Controller\AdminController;
class AreaController extends AdminController
{
    function _initialize() {
        parent::_initialize();
        $this->assign('sys', 'setting'); // 数据集
    }
    
    //省市区(配送地区)
    public function index(){
        $parent_id = I('area_id');
        if(empty($parent_id)){
            $where['area_parent_id'] = 0;
        }else{
            $where['area_parent_id'] = $parent_id;
        }
        
        $arr = M('area')->where($where)->select();
        foreach ($arr as $key => $value) {
            $arr[$key]['count'] = M('area')->where('area_parent_id='.$value['area_id'])->count();
        }
        
        $this->assign("arr", $arr);
        $this->assign('action', 'area');
        $this->display();
    }

    //新增下级
    public function add(){
        $res['area_parent_id'] = I('get.area_id');
        $res['area_deep'] = I('get.area_deep');
        $this->assign('res',$res);
        if(IS_POST){
            $data = I('post.');
            $area = M('Area')->data($data)->add();
            $area ?  $this->success('操作成功', U('index')) : $this->error('操作失败');
        }
        $this->assign('action', 'area');
        $this->display();
    }

    //编辑(修改)
    public function save(){
        $where['area_id'] = I('area_id');
        $area = M('Area');
        $res = $area->where($where)->find();
        $this->assign('res',$res);
        if(IS_POST){
            $where['area_id'] = I('post.area_id');
            $data['area_name'] = I('post.area_name');
            $data['area_sort'] = I('post.area_sort');
            $tf = $area->where($where)->data($data)->save();
            $tf !== null ? $this->success('操作成功', U('index')) : $this->error('操作失败');
        }
        $this->assign('action', 'area');
        $this->display();
    }
}
